package com.feel.easycloud.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feel.easycloud.entity.SysUser;
import com.feel.easycloud.mapper.SysUserMapper;
import com.feel.easycloud.vo.login.LoginJwtVo;
import com.feel.easycloud.vo.login.LoinIdentityRequestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PhoneLoginHandler implements LoginHandler {

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public LoginJwtVo login(LoinIdentityRequestVo requestVo) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",requestVo.getIdentity());
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);

        if(!requestVo.getSecretToken().equals(sysUser.getPassword())){
            throw new RuntimeException("登录账号或密码错误");
        }

        LoginJwtVo loginJwt = new LoginJwtVo();
        loginJwt.setUserName(sysUser.getName());
        loginJwt.setUserId(sysUser.getId());
        return loginJwt;
    }
}
